package com.example.demo.sxt;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;


/**
 * @author : zhy
 * @since : 2022/7/19
 */
public class GameWin extends JFrame {

    //分数
    public int score=0;
    //窗口宽高
    int winWidth=800;
    int winHeight=600;

    //游戏状态 0.未开始 1.游戏中 2.暂停 3.失败 4.通关 5.失败后重启 6.下一关
    public static int state=0;

    //定义双缓存图片
    Image offScreenImage=null;

    //创建蛇头对象
    public HeadObj headObj=new HeadObj(GameUtil.rightImg,60,570,this);

    //定义蛇身集合
    public List<BodyObj> bodyObjList=new ArrayList<>();

    //获取食物类对象
    public FoodObj foodObj=new FoodObj().getFood();

    public void launch(){
        //设置窗口是否可见
        this.setVisible(true);
        //设置窗口的大小
        this.setSize(winWidth,winHeight);
        //设置窗口的位置--屏幕居中
        this.setLocationRelativeTo(null);
        //设置窗口的标题
        this.setTitle("贪食蛇");

        //蛇身的初始化
        bodyObjList.add(new BodyObj(GameUtil.bodyImg,30,570,this));
        bodyObjList.add(new BodyObj(GameUtil.bodyImg,0,570,this));
        //键盘事件
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode()==KeyEvent.VK_SPACE){
                    switch (state){
                        //未开始转开始
                        case 0:state=1;
                        break;
                        //开始转暂停
                        case 1:state=2;
                        repaint();
                        //暂停转开始
                        break;
                        case 2:state=1;
                        break;
                        //失败后重新启动游戏
                        case 3:state=5;
                        break;
                        //下一关
                        case 4:state=6;
                        break;
                        default:
                            break;
                    }
                }
            }
        });
        while(true)
        {
            if(state==1){
                repaint();
            }
            //失败后重启
            if(state==5){
                state=0;
                resetGame();
            }

            if(state==6&&GameUtil.level!=3){
                state=1;
                GameUtil.level++;
                resetGame();
            }
            try {
                if(GameUtil.level==1){
                    Thread.sleep(200);
                }else if(GameUtil.level==2){
                    Thread.sleep(170);
                }else if(GameUtil.level==3){
                    Thread.sleep(150);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    //绘制提示语
    void prompt(Graphics g){
        //未开始
        if(state==0){
            g.fillRect(120,240,400,70);
            GameUtil.drawWord(g,"按下空格键开始游戏",Color.yellow,35,150,290);
        }
        if(state==2){
            g.fillRect(120,240,400,70);
            GameUtil.drawWord(g,"按下空格键继续游戏",Color.yellow,35,150,290);
        }
        if(state==3){
            g.fillRect(120,240,400,70);
            GameUtil.drawWord(g,"咬到自己，游戏失败",Color.red,35,150,290);
        }
        if(state==4){
            g.fillRect(120,240,400,70);
            if(GameUtil.level==3){
                GameUtil.drawWord(g,"达成条件，游戏通关",Color.green,35,150,290);
            }else {
                GameUtil.drawWord(g,"达成条件，按空格键下一关",Color.green,35,150,290);
            }

        }
        if(state==5){
            g.fillRect(120,240,400,70);
            GameUtil.drawWord(g,"游戏失败，按空格键重新开始游戏",Color.green,35,150,290);
        }
    }

    //未开始


    /**
     * 重写绘画方法，在窗口中绘制小方格
     * @param g
     */
    @Override
    public void paint(Graphics g) {
        //初始化双缓存图片
        if(offScreenImage==null){
            offScreenImage=this.createImage(winWidth,winHeight);
        }
        //获取图片对应的Graphics对象
        Graphics gOmage=offScreenImage.getGraphics();
        //灰色背景
        gOmage.setColor(Color.gray);
        //绘制矩形
        gOmage.fillRect(0,0,winWidth,winHeight);
        //绘制网格线
        gOmage.setColor(Color.black);
        //绘制横线，这这四个参数代表两个坐标点，起始点
        for(int i=0;i<=20;i++)
        {
            gOmage.drawLine(0,i*30,600,i*30);
            gOmage.drawLine(i*30,0,i*30,600);
        }
        //绘制蛇身
        for(int i=bodyObjList.size()-1;i>=0;i--)
        {
            bodyObjList.get(i).paintSelf(gOmage);
        }
        //绘制蛇头
        headObj.paintSelf(gOmage);
        //绘制食物
        foodObj.paintSelf(gOmage);
        //绘制关卡文字
        GameUtil.drawWord(gOmage,"第"+GameUtil.level+"关",Color.orange,40,650,260);
        //绘制分数
        GameUtil.drawWord(gOmage,score+" 分",Color.blue,50,650,330);
        //绘制提示语
        gOmage.setColor(Color.GRAY);
        prompt(gOmage);
        //将双缓存图片绘制到窗口中
        g.drawImage(offScreenImage,0,0,null);
    }

    //游戏重置
    void resetGame(){
        //关闭当前窗口
        this.dispose();
        //开启一个新窗口
        String[]args={};
        main(args);
    }






    public static void main(String[] args) {
        GameWin gameWin = new GameWin();
        gameWin.launch();

    }

}
